package com.learning.imoney;

import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.learning.imoney.db.DataBaseBill;

import java.util.Calendar;

public class BillAdd extends AppCompatActivity implements View.OnClickListener {
    private TextView dateText;
    private EditText moneyText,noteText;
    private RadioGroup rg;
    private Button saveBtn,dateBtn,backBtn;
    private DataBaseBill mhelper;//定义数据库帮助类对象
    private SQLiteDatabase db;//定义一个可以操作的数据库对象
    private Calendar calendar = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add_bill);
        moneyText = findViewById(R.id.money_text);
        dateText = findViewById(R.id.date_text);
        noteText = findViewById(R.id.note_text);
        rg = findViewById(R.id.rg1);
        saveBtn = findViewById(R.id.btn_save);
        dateBtn = findViewById(R.id.pick_button);
        backBtn = findViewById(R.id.back_button);

        //按钮监听
        saveBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        dateBtn.setOnClickListener(this);
        dateText.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_button) {
            finish();
        } else if (v.getId() == R.id.btn_save) {
            saveEvent();
        } else if (v.getId() == R.id.pick_button || v.getId() == R.id.date_text) {
            datePick(v);
        }
    }

    private void datePick(View v) {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(BillAdd.this,
                (view, year1, monthOfYear, dayOfMonth) -> {
                    calendar.set(Calendar.YEAR, year1);
                    calendar.set(Calendar.MONTH, monthOfYear);
                    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    dateText.setText(DateFormat.format("yyyy-MM-dd", calendar.getTime()));
                }, year, month, day);
        datePickerDialog.show();
    }

    private void saveEvent() {
        //创建或获取数据库
        mhelper = new DataBaseBill(BillAdd.this);//实例化数据库帮助类
        db = mhelper.getWritableDatabase();//创建数据库，获取数据库的读写权限
        int repeatOptionId = rg.getCheckedRadioButtonId();

        ContentValues values = new ContentValues();//用 value 表示一行
        values.put("money", moneyText.getText().toString());//
        values.put("time",dateText.getText().toString());//
        values.put("note",noteText.getText().toString());//
        if (repeatOptionId == R.id.income){
            values.put("type",1);//
        } else if (repeatOptionId == R.id.expenditures) {
            values.put("type",0);//
        }
        db.insert("bill_info",null,values);
        //将这一行数据存放到数据库的数据表中。参数：（表名，某些为空的列自动赋值 null，ContentValue 对象）
        finish();

    }
}